package com.fieldchain.ex.fa100.mvp.model;

import android.content.Context;
import android.content.Intent;

import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.bean.C2CBean;
import com.fieldchain.ex.fa100.bean.C2CBuyBean;
import com.fieldchain.ex.fa100.bean.User;
import com.fieldchain.ex.fa100.custom.dialog.C2CDialog;
import com.fieldchain.ex.fa100.inter.BoolMsgCallBack;
import com.fieldchain.ex.fa100.inter.IntegerCallBack;
import com.fieldchain.ex.fa100.mvp.base.MyBaseModel;
import com.fieldchain.ex.fa100.mvp.callback.C2CBuyCallBack;
import com.fieldchain.ex.fa100.mvp.callback.C2CSimpleCallBack;
import com.fieldchain.ex.fa100.ui.personal.activity.AddBankActivity;
import com.fieldchain.ex.fa100.ui.personal.activity.AuthActivity;
import com.fieldchain.ex.fa100.ui.personal.activity.SetTradPwdActivity;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.common.UpdateUser;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;

import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/18
 */
public class C2CBuyModle extends MyBaseModel {
    private C2CDialog dialog;
    private User user;

    public void showDialog(final Context mContext, int t1, int t2, int t3) {
        if (dialog != null) {
            dialog.dismiss();
            dialog=null;
        }
        dialog = new C2CDialog(mContext, new IntegerCallBack() {
            @Override
            public void index(int pos) {
                if (pos == 1) {
                    mContext.startActivity(new Intent(mContext, SetTradPwdActivity.class));
                } else if (pos == 2) {
                    mContext.startActivity(new Intent(mContext, AuthActivity.class));
                } else if (pos == 3) {
                    if (user.getTpwFlag() != 1) {
                        MyToast.show("未设置交易密码");
                        return;
                    }
                    if (user.getIdCardAuth() != 1) {
                        MyToast.show("未实名认证");
                        return;
                    }
                    mContext.startActivity(new Intent(mContext, AddBankActivity.class));
                }
            }
        });
        dialog.show();
        dialog.setTags(t1, t2, t3);

    }

    public void updateUser(final Context context, final BoolMsgCallBack callBack) {
        UpdateUser.update(new IntegerCallBack() {
            @Override
            public void index(int pos) {
                user = ShareManager.getInstance().getUser();
                if (pos == 403 || user == null) {
                    callBack.receive(false, "登录/注册");
                } else {
                    callBack.receive(true, "立即买入");
                }
                if (user != null&&pos!=403) {
                    int tpwFlag = user.getTpwFlag();
                    int idCardAuth = user.getIdCardAuth();
                    int bankCardFlag = user.getBankCardFlag();
                    if ((tpwFlag + idCardAuth ) < 2) {
                        showDialog(context, tpwFlag, idCardAuth, bankCardFlag);
                    }
                }

            }
        });
    }

    public void getData(final C2CSimpleCallBack callBack) {
        HttpRequest.getInstance().getRequest(Constant.c2cConfig, new BeanRequest<C2CBean>() {
            @Override
            public void onSucess(C2CBean bean, int code, String msg) {
                if (code == 200) {
                    callBack.receiveData(bean, code);
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void recharge(String amount, String password, final C2CBuyCallBack callBack) {
        if (amount.length() == 0) {
            MyToast.show("请输入充值数量");
            return;
        }
        if (password.length() == 0) {
            MyToast.show("请输入交易密码");
            return;
        }
        HashMap map = new HashMap();
        map.put("amount", amount);
        map.put("password", password);

        HttpRequest.getInstance().postRequest(map, Constant.c2cRecharge, new BeanRequest<C2CBuyBean>() {
            @Override
            public void onSucess(C2CBuyBean c2CBuyBean, int code, String msg) {
                MyToast.show(msg);
                callBack.requestCallBack(c2CBuyBean, code);
            }

            @Override
            public void onFaild(Exception e) {
                callBack.requestCallBack(null, 505);
            }
        });
    }

    public void unShowDialog() {
        if (dialog == null) {
            return;
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void detachView() {
        if (dialog == null) {
            return;
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = null;
    }
}
